package com.mskj.zxmy.mapper;

import com.mskj.zxmy.dto.CKDanSaveDTO;
import com.mskj.zxmy.dto.ProductDetailDTO;
import com.mskj.zxmy.entity.*;
import com.mskj.zxmy.dto.StockInDetailSaveDTO;
import com.mskj.zxmy.dto.RealTimeStockDTO;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface StockInMapper {

    @Select("select batchBarCodes from barcodeManagementTable where id = #{batchNumberId}")
     String getbatchNumberByBarcode(Integer batchNumberId);


    //查询入库单最大批次号

    @Select("SELECT MAX(singleBatch) FROM  whmskj_ZXMY.ck03Listing WHERE singleBatch LIKE 'PC${datePart}%'")
    String selectMaxBatchNo(String datePart);

    //查询入库单最大单据号
//    @Select("select MAX(billNo) from whmskj_ZXMY.ck03Listing where billNo like 'RK${datePart}%'")
//    String selectMaxBatchNo(String datePart);

    @Select("SELECT * FROM ck03Listing WHERE billNo = #{billNo}")
    StockIn selectByBillNo(String billNo);

    /**
     * 插入入库明细子表
     */
    int insertStockInDetail(StockInDetailSaveDTO detail);

    /**
     * 根据id查询入库单详情
     */
    StockIn selectById(String id);

    /**
     * 更新入库单状态
     */
    int updateStatusById(String id, String status);

    /**
     * 插入入库单主表
     */
    int insertStockIn(StockIn stockIn);

    /**
     * 查询最大入库单号
     * @param datePart
     * @return
     */
    String selectMaxRepositoryNumberByDate(String datePart);

    /**
     * 入库状态查询入库单列表
     * @return
     */
    @Select("SELECT * FROM ck03Listing where documentType='生产入库' and repositoryStatus=#{repositoryStatus} order by repositoryDate desc")
    List<Ck03Listing> selectAllStockIn(String  repositoryStatus);

    /**
     * 根据入库单查询入库
     * @param repositoryNumber
     * @return
     */
    List<StockIn> selectStockInByNumber(@Param("repositoryNumber") String repositoryNumber);

    /**
     * 根据入库单ID查询子表详情
     * @param stockInId
     * @return
     */
    List<com.mskj.zxmy.dto.ProductDetailDTO> selectDetailsByStockInId(@Param("id") String stockInId);

    /**
     * 根据批次号插寻产品信息（出库信息明细）
     * @param
     * @return
     */
    ProductDetailDTO getProductInfoByDetailId(String detailId);

    StockInDetailSaveDTO selectStockInDetailById(Integer detailId);
    // 通过产品id查物品信息
    ProductDetailDTO selectProductDetailByProductId(String productId);


    @Select("select Id from supplyManagement where nameOfSupplier = #{associatedSuppliers}")
    String getAssociatedSuppliersId(String associatedSuppliers);


    @Select("select m.nick from corp_member m left join ck03Listing c on m.memberId = c.operator where id = #{id}")
    String getAssociatedSuppliersName(Integer id);

    /**
     * 保存入库明细，返回插入行数
     */
    int saveDetail(StockInDetailSaveDTO detail);


    @Select("select * from gy02ProductRepository where id = #{id}")
    ProductDetailDTO getProductInfoByNumber(Integer id);

    @Select("select repositoryNumber from ck03Listing where id = #{linkToListOfId}")
    String getlinkToListByrepositoryNumber(String linkToListOfId);

    List<CKDanSaveDTO> getCKDanDetailsByXSDan(String xsd);

    /**
     * 根据批次号确认入库明细，更新状态和入库数量
     */


    /**
     * 实时库存查询，按产品和批次分组
     */
    List<RealTimeStockDTO> getRealTimeStock(@Param("productNumber") String productNumber);

    /**
     * 多条件实时库存查询，按产品编号、型号、名称、批次分组统计
     */
    List<RealTimeStockDTO> getRealTimeStockByCondition(@Param("productNumber") String productNumber,
                                                      @Param("productName") String productName,
                                                      @Param("productSpecifications") String productSpecifications,
                                                      @Param("typeOfProduct") String typeOfProduct,
                                                      @Param("environmentalProtectionLevel") String environmentalProtectionLevel,
                                                      @Param("brand") String brand,
                                                      @Param("batchNumber") String batchNumber,
                                                      @Param("materialCategory") String materialCategory);

    @Select("select nick from corp_member where memberId = #{loginId}")
    String getUserName(String loginId);


//    根据工单id查询任务数量
    @Select("select numberOfTasks from missionInformationSc02 where id= #{id}")
    int getNumber(String id);

//    根据任务工单id查询任务工单号
    @Select("select missionStream from missionInformationSc02 where id =#{id}")
    String getProductionTasksById(Integer id);

    @Select("select processName from sctechMissions where id = CAST(#{tasks} AS UNSIGNED)")
    String getTasksById(String tasks);

    boolean updateStatusByDetailId(String batchNumber,Integer numberOfEntries,String scantobaseStatus);


    int updateRepositoryStatusByNumber(@Param("repositoryNumber") String repositoryNumber, @Param("repositoryStatus") String repositoryStatus);

    int updateStockInDetailQuantity(@Param("detailId") Integer detailId, @Param("numberOfEntries") Integer numberOfEntries);

    String getScantobaseStatusByDetailId(@Param("detailId") Integer detailId);

    /**
     * 根据menberId查询姓名
     * @param synchronisingFolderFailedSS
     * @return
     */
    @Select("select nick from corp_member where memberId =#{synchronisingFolderFailedSS}")
    String getUserNameBymenmberId(String synchronisingFolderFailedSS);

    /**
     * 根据meberId查询UserId
     * @param MemberId
     * @return
     */
    @Select("select userId from corp_member where memberId = #{MemberId}")
    String getUserIdByMemberId(String MemberId);

    List<StockInDetailSaveDTO> getStockInDetails(String repositoryStatus);

    List<StockInDetailSaveDTO> selectStockInByProductNumbers(@Param("repositoryNumber") String repositoryNumber, @Param("list") List<String> productNumbers ,@Param("repositoryStatus") String repositoryStatus);


    //入库单号查询批次号
    @Select("select singleBatch from ck03Listing where repositoryNumber = #{repositoryNumber}")
    String selectPCNumber(String repositoryNumber);

//    通过批次号更新主条码表
    void updateBybarcodeManagementTable(@Param("barcode") BarcodeManagement barcodeManagement);

    @Select("select * from ck03Listing where repositoryNumber=#{repositoryNumber};")
    StockIn selectStockInByRepNumber(String repositoryNumber);


    @Select("select * from sctechMissions where id = #{tasks}")
    SctechMissions getcurrentProcessTaskById(Integer tasks);

    @Select("select id from barcodeManagementTable where batchBarCodes=#{batchBarCodes}")
    Integer getBarcodeManagementByPcNumber(String batchBarCodes);

    void savaBarCodeManagementDetails(@Param("barCodeDetails") BarCodeManagementDetails barCodeManagementDetails);

    // 入库单未入库数量
    int countUnStockedIn();

    @Select("select * from barcodeManagementTable where id =#{detailId}")
    BarcodeManagement getBarcodeManagementBydetailId(Integer detailId);

    @Select("select id from ck04ListOfEntriesListOfEntries where batchNumber = #{batchBarCodes}")
    Integer getStockInDetailId(String batchBarCodes);


    int countUnStockedInDetails();
}
